package com.jamlu.framework.base;

import android.app.Dialog;
import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jamlu.framework.presenter.IBaseRxPresenter;
import com.jamlu.framework.ui.status.BaseStatusView;
import com.jamlu.framework.ui.view.IBaseView;
import com.jamlu.framework.ui.status.IStatusView;
import com.jamlu.framework.ui.widget.MyProgressDialog;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by DMing on 2018/02/23
 */
public abstract class BaseRxFragment<T extends IBaseRxPresenter> extends Fragment implements IBaseView {

    protected T presenter;
    private MyProgressDialog mProgressDialog;//加载对话框
    private BaseStatusView<T> baseStatusView;
    private Unbinder unbinder;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        ViewGroup mRootView = (ViewGroup) LayoutInflater.from(getActivity()).inflate(getLayoutResID(), container, false);
        presenter = createPresenter();
        unbinder = ButterKnife.bind(this, mRootView);
        initViews(savedInstanceState);
        initData();
        initEvent();
        return mRootView;
    }


    @Override
    public Context getContext() {
        return getActivity();
    }

    @Override
    public IStatusView getStatusView(){
        if (baseStatusView == null)baseStatusView = new BaseStatusView<>(this);
        if (baseStatusView.getPresenter() == null)baseStatusView.setPresenter(presenter);
        return baseStatusView;
    }


    /**
     * 实现setContentView()
     */
    @LayoutRes
    protected abstract int getLayoutResID();


    /**
     *
     * 获取P层实现类
     */
    public T getPresenter(){
        return presenter;
    }

    /**
     * 初始化P层实现类
     * @return Presenter
     */
    protected abstract T createPresenter();


    /**
     * 显示加载对话框
     * @param cancelable 是否能点击取消
     */
    @Override
    public void showProgressDialog(Dialog dialog,boolean cancelable) {
        dialog.setCancelable(cancelable);
        dialog.show();
    }

    /**
     * 隐藏加载对话框
     */
    @Override
    public void dismissProgressDialog(Dialog dialog) {
        if (dialog != null) {
            dialog.dismiss();
        }
    }

    /**
     * 初始化对话框
     */
    @Override
    public Dialog createNewProgressDialog() {
        mProgressDialog = new MyProgressDialog(getActivity());
        mProgressDialog.setCanceledOnTouchOutside(false);
        return mProgressDialog;
    }


    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (hidden) {
            if (presenter != null)presenter.disposeAll();
        }
    }

    @Override
    public void onDestroyView() {
        if (presenter != null)presenter.disposeAll();
        dismissProgressDialog(mProgressDialog);
        super.onDestroyView();
        unbinder.unbind();//解绑
    }

    /**
     * 初始化布局组件
     */
    protected void initViews(Bundle savedInstanceState) {

    }

    /**
     * 初始化布局组件的数据
     */
    protected void initData(){

    }

    /**
     * 初始化布局组件的监听事件
     */
    protected void initEvent(){

    }

}
